﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FlashPay.Service.Impl
{
    using FlashPay.DAO.Bank;
    using FlashPay.DAO.Interface;
    using FlashPay.DAO.Sys;
    using FlashPay.Entity;
    using FlashPay.Entity.Request.PaymentInterface;
    using FlashPay.Entity.Response.Bank;
    using FlashPay.Entity.Response.Company;
    using FlashPay.Entity.Response.Sys;
    using FlashPay.Service.Interface;
    using System.Linq;

    /// <summary>
    /// 第三方接口接口实现
    /// </summary>
    public class PaymentInterfaceServiceImpl: PaymentInterfaceService
    {
        #region 注入
        /// <summary>
        /// 用户数据接口
        /// </summary>
        private readonly PaymentInterfaceDao _paymentInterfaceDao;

        /// <summary>
        /// 系统配置接口
        /// </summary>
        private readonly SysConfigDao _sysConfigDao;

        /// <summary>
        /// 银行数据接口
        /// </summary>
        private readonly BankDao _bankDao;

        /// <summary>
        /// 银行数据接口
        /// </summary>
        public readonly BankInfoDAO _bankInfoDao;

        /// <summary>
        /// 注入
        /// </summary>
        /// <param name="menu">菜单数据接口</param>
        public PaymentInterfaceServiceImpl(PaymentInterfaceDao paymentInterfaceDao, SysConfigDao sysConfigDao, BankDao bankDao, BankInfoDAO bankInfoDao)
        {
            _paymentInterfaceDao = paymentInterfaceDao;
            _sysConfigDao = sysConfigDao;
            _bankDao = bankDao;
            _bankInfoDao = bankInfoDao;
        }
        #endregion

        /// <summary>
        /// 根据编号获取公司
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>Company</returns>
        public List<CompanyResponse> GetCompany(int companyId, int level = 2)
        {
            return _bankDao.GetCompany(companyId, level);
        }

        /// <summary>
        /// 根据编号付款接口
        /// </summary>
        public PaymentInterfaceReponse GetByPaymentInterface(int paymentInterfaceId,int companyId, List<string> userPermission)
        {
            var paymentInterfaceReponse = new PaymentInterfaceReponse();

            var paymentInterface = _paymentInterfaceDao.GetByPaymentInterface(paymentInterfaceId);
            if (paymentInterface != null) {
                paymentInterfaceReponse.PaymentInterfaceID = paymentInterface.PaymentInterfaceID;
                paymentInterfaceReponse.CompanyID = paymentInterface.CompanyID;
                paymentInterfaceReponse.PaymentType = paymentInterface.PaymentType;
                paymentInterfaceReponse.CompanyName = paymentInterface.CompanyName;
                paymentInterfaceReponse.SecretKey = paymentInterface.SecretKey;
                paymentInterfaceReponse.PaymentStart = paymentInterface.PaymentStart.ToString();
                paymentInterfaceReponse.PaymentEnd = paymentInterface.PaymentEnd.ToString();
                paymentInterfaceReponse.WithdrawalBank = paymentInterface.WithdrawalBank;
                paymentInterfaceReponse.ExcludedWithdrawalBank = paymentInterface.ExcludedWithdrawalBank;
                paymentInterfaceReponse.DepositType = paymentInterface.DepositType;
                paymentInterfaceReponse.PaymentMax = paymentInterface.PaymentMax;
                paymentInterfaceReponse.SortNo = paymentInterface.SortNo;
                if (paymentInterface.LimitOpenDate.HasValue) {
                    paymentInterfaceReponse.LimitOpenDate = paymentInterface.LimitOpenDate.Value.ToString("HH:mm");
                }
                if (paymentInterface.LimitCloseDate.HasValue)
                {
                    paymentInterfaceReponse.LimitCloseDate = paymentInterface.LimitCloseDate.Value.ToString("HH:mm");
                }
                paymentInterfaceReponse.LimitRepeat = paymentInterface.LimitRepeat;
                paymentInterfaceReponse.LimitStatus = paymentInterface.LimitStatus;
                paymentInterfaceReponse.EnableBalanceCheck = paymentInterface.EnableBalanceCheck;
                paymentInterfaceReponse.Balance = paymentInterface.Balance.ToString("#0.00");
                if (userPermission.Contains("PaymentInterface0005"))
                {
                    paymentInterfaceReponse.SecretKeyPermission = "PaymentInterface0005";
                }
            }

            #region 银行
            var bankInfoResponses = new List<BankInfoResponse>();

            var bankInfos = _bankInfoDao.GetBankInfoList();
            if (bankInfos != null && bankInfos.Any())
            {
                bankInfos.ForEach(item =>
                {
                    bankInfoResponses.Add(new BankInfoResponse()
                    {
                        BankCode = item.BankCode,
                        BankName = item.BankName
                    });
                });

                paymentInterfaceReponse.BankInfoResponse = bankInfoResponses;
            }
            #endregion

            #region 公司列表
            var companys = this.GetCompany(companyId);

            var companyResponse = new List<CompanyResponse>();
            if (companys != null && companys.Any())
            {
                foreach (var item in companys)
                {
                    companyResponse.Add(new CompanyResponse()
                    {
                        CompanyID = item.CompanyID,
                        CompanyName = item.CompanyName,
                    });
                }
                paymentInterfaceReponse.CompanyResponse = companyResponse;
            }
            #endregion

            #region 配置列表
            List<SysConfigResponse> SysConfigTypeList = new List<SysConfigResponse>();
            var q = _sysConfigDao.GetTransTypeList(new FlashPay.Entity.Request.Sys.SysConfigAddOrEditRequest { ConfigCode = "PaymentInterface" });//配置列表
            q.ForEach(item =>
            {
                SysConfigTypeList.Add(new SysConfigResponse()
                {
                    ConfigId = item.ConfigId,
                    ConfigCode = item.ConfigCode,
                    ConfigValue = item.ConfigValue,
                    ConfigContent = item.ConfigContent,
                    CompanyId = item.CompanyId,
                    Description = item.Description
                });
            });
            paymentInterfaceReponse.SysConfigTypeList = SysConfigTypeList;
            #endregion 配置列表

            return paymentInterfaceReponse;
        }

        /// <summary>
        /// 新增或更新付款接口
        /// </summary>
        /// <param name="CardMerchant">修改对象</param>
        /// <returns></returns>
        public JResult PaymentInterfaceAddOrUpdate(PaymentInterfaceAddOrEditRequest request)
        {
            return _paymentInterfaceDao.PaymentInterfaceAddOrUpdate(request);
        }

        /// <summary>
        /// 增加、减少余额
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JResult PaymentInterfaceBalance(PaymentInterfaceBalanceRequest request)
        {
            return _paymentInterfaceDao.PaymentInterfaceBalance(request);
        }
    }
}
